package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.*;
import com.example.project.entity.Watcher;

/**
 * 用户关注Mapper接口
 * 
 * @author ruoyi
 * @date 2025-11-06
 */
public interface WatcherMapper 
{
    /**
     * 查询用户关注
     * 
     * @param id 用户关注主键
     * @return 用户关注
     */
    @Select("select id, watchable_type, watchable_id, user_id, create_time, update_time from t_watcher where id = #{id}")
    public Watcher selectWatcherById(Long id);

    /**
     * 查询用户关注列表
     * 
     * @param watcher 用户关注
     * @return 用户关注集合
     */
    @Select("<script>" +
            "select id, watchable_type, watchable_id, user_id, create_time, update_time from t_watcher" +
            "<where>" +
            "<if test='watchableType != null and watchableType != \"\"'> and watchable_type = #{watchableType}</if>" +
            "<if test='watchableId != null and watchableId != \"\"'> and watchable_id = #{watchableId}</if>" +
            "<if test='userId != null and userId != \"\"'> and user_id = #{userId}</if>" +
            "<if test='createTime != null'> and create_time = #{createTime}</if>" +
            "<if test='upTimeeTime != null'> and update_time = #{updateTime}</if>" +
            "</where>" +
            "</script>")
    public List<Watcher> selectWatcherList(Watcher watcher);

    /**
     * 新增用户关注
     * 
     * @param watcher 用户关注
     * @return 结果
     */
    @Insert("<script>" +
            "insert into t_watcher" +
            "<trim prefix='(' suffix=')' suffixOverrides=','>" +
            "<if test='watchableType != null and watchableType != \"\"'>watchable_type,</if>" +
            "<if test='watchableId != null and watchableId != \"\"'>watchable_id,</if>" +
            "<if test='userId != null and userId != \"\"'>user_id,</if>" +
            "<if test='createTime != null'>create_time,</if>" +
            "<if test='updateTime != null'>update_time,</if>" +
            "</trim>" +
            "<trim prefix='values (' suffix=')' suffixOverrides=','>" +
            "<if test='watchableType != null and watchableType != \"\"'>#{watchableType},</if>" +
            "<if test='watchableId != null and watchableId != \"\"'>#{watchableId},</if>" +
            "<if test='userId != null and userId != \"\"'>#{userId},</if>" +
            "<if test='createTime != null'>#{createTime},</if>" +
            "<if test='updateTime != null'>#{updateTime},</if>" +
            "</trim>" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public int insertWatcher(Watcher watcher);

    /**
     * 修改用户关注
     * 
     * @param watcher 用户关注
     * @return 结果
     */
    @Update("<script>" +
            "update t_watcher" +
            "<trim prefix='SET' suffixOverrides=','>" +
            "<if test='watchableType != null and watchableType != \"\"'>watchable_type = #{watchableType},</if>" +
            "<if test='watchableId != null and watchableId != \"\"'>watchable_id = #{watchableId},</if>" +
            "<if test='userId != null and userId != \"\"'>user_id = #{userId},</if>" +
            "<if test='createTime != null'>create_time = #{createTime},</if>" +
            "<if test='updateTime != null'>update_time = #{updateTime},</if>" +
            "</trim>" +
            "where id = #{id}" +
            "</script>")
    public int updateWatcher(Watcher watcher);

    /**
     * 删除用户关注
     * 
     * @param id 用户关注主键
     * @return 结果
     */
    @Delete("delete from t_watcher where id = #{id}")
    public int deleteWatcherById(Long id);

    /**
     * 批量删除用户关注
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Delete("<script>" +
            "delete from t_watcher where id in" +
            "<foreach item='id' collection='array' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    public int deleteWatcherByIds(String[] ids);
    
    /**
     * 根据条件查询用户关注
     * 
     * @param watchableType 被关注对象类型
     * @param watchableId 被关注对象ID
     * @param userId 用户ID
     * @return 用户关注
     */
    @Select("select id, watchable_type, watchable_id, user_id, create_time, update_time from t_watcher where watchable_type = #{watchableType} and watchable_id = #{watchableId} and user_id = #{userId}")
    public Watcher selectWatcherByConditions(String watchableType, Long watchableId, Long userId);
    
    /**
     * 根据对象查询关注列表
     * 
     * @param watchableType 被关注对象类型
     * @param watchableId 被关注对象ID
     * @return 关注列表
     */
    @Select("select id, watchable_type, watchable_id, user_id, create_time, update_time from t_watcher where watchable_type = #{watchableType} and watchable_id = #{watchableId}")
    public List<Watcher> selectWatchersByObject(String watchableType, Long watchableId);
    
    /**
     * 根据对象删除所有关注
     * 
     * @param watchableType 被关注对象类型
     * @param watchableId 被关注对象ID
     * @return 结果
     */
    @Delete("delete from t_watcher where watchable_type = #{watchableType} and watchable_id = #{watchableId}")
    public int deleteWatcherByObject(String watchableType, Long watchableId);
    
    /**
     * 根据条件删除用户关注
     * 
     * @param watchableType 被关注对象类型
     * @param watchableId 被关注对象ID
     * @param userId 用户ID
     * @return 结果
     */
    @Delete("delete from t_watcher where watchable_type = #{watchableType} and watchable_id = #{watchableId} and user_id = #{userId}")
    public int deleteWatcherByConditions(String watchableType, Long watchableId, Long userId);
}
